<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.interfaces.cese.duss.ceseutil.CostUtil" %>
<%@ page import="java.math.BigDecimal" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>
<%

    String yshd = Util.null2String( request.getParameter( "yshd" ) );
    String djrq = Util.null2String( request.getParameter( "djrq" ) );//单据日期
    String cblrzx = Util.null2String( request.getParameter( "cblrzx" ) );
    String requestid = Util.null2String( request.getParameter( "requestid" ) );
    bb.writeLog( "----------------getExpenseReimbursement_Ajax----requestid="+requestid );
    JSONObject jsonData = new JSONObject();
    String jsonArr = Util.null2String( request.getParameter( "jsonArr" ) );
    bb.writeLog( "jsonArr=" + jsonArr );
    JSONArray jsonArray = JSONArray.parseArray( jsonArr );
    bb.writeLog( "jsonArray=" + jsonArray );
    boolean flag = true;
    String msg = "";
    if (jsonArray.size() > 0) {
        JSONArray newjsonArray = delRepeatIndexid( jsonArray,"fykm","bxje" );
        bb.writeLog( "newjsonArray=" + newjsonArray );
        String yskmName = "";
        for (int i = 0; i < newjsonArray.size(); i++) {
            String fykm = newjsonArray.getJSONObject( i ).getString( "fykm" );////费用科目
            String bxje = newjsonArray.getJSONObject( i ).getString( "bxje" );//报销金额
            BigDecimal newbxje = "".equals( bxje ) ? BigDecimal.valueOf( 0 ) : new BigDecimal( bxje );
            double jdcb = CostUtil.getYszxxKyys( yshd, fykm, cblrzx, djrq );
            double ysje = CostUtil.getYszxxxbYsje( requestid, yshd, fykm, cblrzx, djrq, "0,1" );
            if (jdcb == 0) {
                flag = false;
                String fykmName = CostUtil.getFykmId2Name( fykm );//获取费用科目名称
                yskmName += "".equals( yskmName ) ? fykmName : "," + fykmName;
                msg = "提示：费用科目为[" + yskmName + "]没有季度成本可用预算";
            } else {
                if (BigDecimal.valueOf( jdcb ).subtract( BigDecimal.valueOf( ysje ) ).compareTo( newbxje ) == -1) {
                    flag = false;
                    msg = "提示：报销金额不允许大于季度成本可用预算";
                }
            }
        }
        jsonData.put( "flag", flag );
        jsonData.put( "msg", msg );
    }
    out.print( jsonData.toJSONString() );


    bb.writeLog( "-------------getExpenseReimbursement_Ajax=" + jsonData.toJSONString() );
%>
<%!
    //根据Key去重复合并值
    public static JSONArray delRepeatIndexid(JSONArray array, String kmparam, String jeparam) {
        JSONArray arrayTemp = new JSONArray();
        int num = 0;
        for (int i = 0; i < array.size(); i++) {
            if (num == 0) {
                arrayTemp.add( array.get( i ) );
            } else {
                int numJ = 0;
                for (int j = 0; j < arrayTemp.size(); j++) {
                    JSONObject newJsonObjectI = (JSONObject) array.get( i );
                    JSONObject newJsonObjectJ = (JSONObject) arrayTemp.get( j );
                    String index_idI = newJsonObjectI.get( kmparam ).toString();
                    String valueI = newJsonObjectI.get( jeparam ).toString();
                    String index_idJ = newJsonObjectJ.get( kmparam ).toString();
                    String valueJ = newJsonObjectJ.get( jeparam ).toString();
                    if (index_idI.equals( index_idJ )) {
                        String newValue = new BigDecimal( valueI ).add( new BigDecimal( valueJ ) ).toString();
                        arrayTemp.remove( j );
                        JSONObject newObject = new JSONObject();
                        newObject.put( kmparam, index_idI );
                        newObject.put( jeparam, newValue );
                        arrayTemp.add( newObject );
                        break;
                    }
                    numJ++;
                }
                if ((numJ - 1) == (arrayTemp.size() - 1)) {
                    arrayTemp.add( array.get( i ) );
                }
            }
            num++;
        }
        return arrayTemp;
    }
%>

